package com.example.ivos.dictoption.service.impl;

import com.example.ivos.dict.mapper.DictMapper;
import com.example.ivos.dict.pojo.dto.DictQuery;
import com.example.ivos.dict.pojo.vo.DictVO;
import com.example.ivos.dictoption.mapper.DictOptionMapper;
import com.example.ivos.dictoption.pojo.dto.DictOptionQuery;
import com.example.ivos.dictoption.pojo.dto.DictOptionSaveParam;
import com.example.ivos.dictoption.pojo.entity.DictOption;
import com.example.ivos.dictoption.pojo.vo.DictOptionVO;
import com.example.ivos.dictoption.service.DictOptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DictOptionServiceImpl implements DictOptionService {
    @Autowired
    private DictOptionMapper dictOptionMapper;
    @Autowired
    private DictMapper dictMapper;
    @Override
    public void save(DictOptionSaveParam dictOptionSaveParam) {
        log.debug("保存字典项业务:dictOptionSaveParam={}",dictOptionSaveParam);
        DictOption dictOption = new DictOption();
        BeanUtils.copyProperties(dictOptionSaveParam,dictOption);
        if(dictOption.getId() == null){
            dictOption.setCreateTime(new Date());
            dictOptionMapper.insert(dictOption);
        }else{
            dictOption.setUpdateTime(new Date());
            dictOptionMapper.update(dictOption);
        }
    }

    @Override
    public List<DictOptionVO> selectDictOption(DictOptionQuery dictOptionQuery) {
        log.debug("查询字典项业务参数:{}", dictOptionQuery);
        DictOption dictOption = new DictOption();
        BeanUtils.copyProperties(dictOptionQuery, dictOption);
        List<DictOptionVO> list = dictOptionMapper.select(dictOption);
        return list;
    }

    @Override
    public void delete(Long id) {
        log.debug("删除字典项业务:id={}",id);
        dictOptionMapper.delete(id);
    }

    @Override
    public void deletebyDictId(Long dictId) {
        log.debug("删除字典项业务:dictId={}",dictId);
        dictOptionMapper.deleteByDictId(dictId);
    }

    @Override
    public List<DictOptionVO> selectDictOptionByCode(String code) {
        log.debug("查询字典项业务参数:{}", code);
        DictQuery dictQuery = new DictQuery();
        dictQuery.setCode(code);
        List<DictVO> list = dictMapper.selectDictByCode(dictQuery);
        DictOptionQuery  dictOptionQuery = new DictOptionQuery();
        dictOptionQuery.setDictId(list.get(0).getId());
        return dictOptionMapper.selectbycode(dictOptionQuery);
    }
}